package com.example.demo.controller;

import ch.qos.logback.core.util.StringUtil;
import com.example.demo.constant.Constants;
import com.example.demo.model.*;
import com.example.demo.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/getlist")
    public List<BookInfo> getlsit(){
        List<BookInfo> booklist=bookService.getlist();
        return booklist;
    }
    @RequestMapping("/addbook")
    public String addBook(BookInfo bookInfo){
        if(StringUtil.isNullOrEmpty(bookInfo.getBookName())||
                StringUtil.isNullOrEmpty(bookInfo.getAuthor())||
                StringUtil.isNullOrEmpty(bookInfo.getPublish())||
                bookInfo.getCount()==null||
                bookInfo.getPrice()==null
        ){
            log.info("添加的图书数据：{}",bookInfo);
            return "传入参数不符合";
        }
        try {
            bookService.addBook(bookInfo);
            log.info("添加的图书数据：{}",bookInfo);
            return "成功添加";
        } catch (Exception e) {
            log.info("添加图书失败 e:{}",e);
            return e.getMessage();
        }
    }
    @RequestMapping("/getlistpage")
    public Result<PageResult<BookInfo>> getListPage(PageRequest pageRequest, HttpSession httpSession){
        log.info("数据信息 :{}",pageRequest);
        if(httpSession.getAttribute(Constants.SESSION_USER)==null)
            return Result.unlogin();
        PageResult<BookInfo> pageResult=bookService.getBookListPage(pageRequest);
        UserInfo userInfo= (UserInfo) httpSession.getAttribute(Constants.SESSION_USER);
        if(userInfo.getId()<0||userInfo==null||"".equals(userInfo.getUserName())){
            return Result.unlogin();
        }
        log.info("pageResult:" +pageResult);
        return Result.success(pageResult);
    }

    @RequestMapping("/getmessage")
    public BookInfo getMessage(Integer bookid){
        if(bookid==null||bookid==0)return null;
        BookInfo bookInfo=bookService.getistMessage(bookid);
        log.info("返回的数据:{}",bookInfo);
        return bookInfo;
    }

    @RequestMapping("/getupdate")
    public Result<String> updateBook(BookInfo bookInfo){
        try {
            bookService.updateBook(bookInfo);
            return Result.success(bookInfo);
        } catch (Exception e) {
            log.info("更新图书失败:{}",e);
            return Result.fail("更新图书失败");
        }
    }
    @RequestMapping("/getdelete")
    public boolean delete(Integer bookid){
        try {
            Integer sum=bookService.delete(bookid);
            return true;
        } catch (Exception e) {
            log.info("删除失败:{}",e);
            return false;
        }
    }
    @RequestMapping("getBacthDelete")
    public boolean bacthDeleteBook(@RequestParam List<Integer> ids){
        try {
            bookService.bacthDeleteBook(ids);
            return true;
        } catch (Exception e) {
          log.info("批量删除异常:{}",e);
          return false;
        }
    }
}
